import 'dart:async';
import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';

import 'app_router_handler.dart';
import 'app_routers.dart';

enum PushPage {
  goHome, //去首页
  goHomeAndLogin, //回首页切跳转登录
  goLogin, //登录界面
  goBlackList, //黑明单
  goLimitSale, //限量发售
  goMallHome, //商城首页
  goHomeAndLoginToMall, //回首页切跳转登录
  goMe //个人中心
}

enum PopPage {
  popUp, //返回上一页
  popNone, //留在当前页
  popUntil, //返回到第一页
}

class AppRoutesManager {
  static final StreamController<Map> _appRoutesManageListener =
      StreamController.broadcast(); //界面跳转管理
  static Map<Function, StreamSubscription> listeners = {};

  static FluroRouter router = FluroRouter();

  static void initRouter() {
    if (router == null) {
      router = FluroRouter();
    }
    defineRoutes();
  }

  //添加界面监听
  static void addAppRoutesManageListener(void onData(Map notification)) {
    listeners[onData] = _appRoutesManageListener.stream.listen(onData);
  }

  //界面跳转广播
  static void pushPage(Map<String, Object> map) {
    _appRoutesManageListener.add(map);
  }

  //取消监听
  static void removeListener(void onData(dynamic data)) {
    StreamSubscription? listener = listeners[onData];
    if (listener == null) return;
    listener.cancel();
    listeners.remove(onData);
  }

  // TransitionType.inFromLeft //左侧进入
  //
  // TransitionType.inFromRight //右侧进入
  //
  // TransitionType.inFromBottom //底部进入 默认入场动画
  //
  // TransitionType.native //无进入动画
  //
  // TransitionType.nativeModal //同上
  //
  // TransitionType.fadeIn //渐显动画
  //
  // TransitionType.custom //自定义
  //
  // TransitionType.material //感觉和上面几种一样 无特别动画效果
  //
  // TransitionType.materialFullScreenDialog //感觉和上面几种一样 无特别动画效果
  //
  // TransitionType.cupertino //右进右出
  //
  // TransitionType.cupertinoFullScreenDialog //底部进 底部出 下个页面返回值变化
  //配置路由
  static void defineRoutes() {
    //注册路由
    router.notFoundHandler = NotFoundHandler;
    router.define(
      AppRouters.index,
      handler: IndexPageHandler,
      transitionType: TransitionType.native,
    );
    router.define(
      AppRouters.main,
      handler: MainPageHandler,
      transitionType: TransitionType.native,
    );
    router.define(
      AppRouters.home,
      handler: HomePageHandler,
      transitionType: TransitionType.native,
    );

    router.define(
      AppRouters.user,
      handler: UserPageHandler,
      transitionType: TransitionType.native,
    );
    router.define(
      AppRouters.user2,
      handler: User2PageHandler,
      transitionType: TransitionType.native,
    );
    router.define(
      AppRouters.user3,
      handler: User3PageHandler,
      transitionType: TransitionType.native,
    );
    router.define(
      AppRouters.login,
      handler: LoginPageHandler,
      transitionType: TransitionType.native,
    );

    router.define(
      AppRouters.goodsDetail,
      handler: GoodsDetailsPageHandler,
      transitionType: TransitionType.native,
    );
  }

  static Future navigateTo(BuildContext context, String path,
      {Map<String, dynamic>? params, bool clearStack = false}) {
    if (params != null) {
      path += "?";
      params.forEach((key, value) {
        path += "${key}=${value}&";
      });
    }
    print("----" + path);
    return router.navigateTo(context, path, clearStack: clearStack);
  }
}
